{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import time\n",
    "import numpy as np\n",
    "import CNNIOT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "conv1_w=np.load('LeNet-weights/Conv1_weight.np.npy')\n",
    "conv1_b=np.load('LeNet-weights/Conv1_bias.np.npy')\n",
    "conv2_w=np.load('LeNet-weights/Conv2_weight.np.npy')\n",
    "conv2_b=np.load('LeNet-weights/Conv2_bias.np.npy')\n",
    "f1_w=np.load('LeNet-weights/fc1_weight.np.npy')\n",
    "f2_w=np.load('LeNet-weights/fc2_weight.np.npy')\n",
    "f1_b=np.load('LeNet-weights/fc1_bias.np.npy')\n",
    "f2_b=np.load('LeNet-weights/fc2_bias.np.npy')\n",
    "data=np.load('Mnist/mnist_data.npy')\n",
    "target1=np.load('Mnist/mnist_label.npy')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "conv1=CNNIOT.Convolution2D(1,10,5,5,1,1,0,0,conv1_w,conv1_b,1000000)\n",
    "conv2=CNNIOT.Convolution2D(10,20,5,5,1,1,0,0,conv2_w,conv2_b,1000000)\n",
    "pool1=CNNIOT.Pool(2,2,2,2,'Max',0,1,10000)\n",
    "fc1=CNNIOT.FC(320,50,1,f1_w,f1_b)\n",
    "fc2=CNNIOT.FC(50,10,1,f2_w,f2_b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "total = 0\n",
    "correct = 0\n",
    "data2 = data[0:300]\n",
    "target=target1[0:300]\n",
    "size = data2.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inference Time 1.5375370979309082\n",
      "accuracy= 1.0\n"
     ]
    }
   ],
   "source": [
    "def test(testnumber):\n",
    "    total = 0\n",
    "    correct = 0\n",
    "    data2 = data[0:testnumber]\n",
    "    target=target1[0:testnumber]\n",
    "    size = data2.shape\n",
    "    t1=time.time()\n",
    "    rs = np.zeros((size[0],10))\n",
    "    for i0 in range(0, size[0]):\n",
    "        x=conv1.forward(data2[i0],CNNIOT.dma)\n",
    "        x= pool1.forward(x,CNNIOT.dma)\n",
    "        x=conv2.forward(x,CNNIOT.dma)\n",
    "        x= pool1.forward(x,CNNIOT.dma)\n",
    "        x=fc1.forward(x,CNNIOT.dma)\n",
    "        rs[i0]=fc2.forward(x,CNNIOT.dma)\n",
    "\n",
    "    for i in range(0, size[0]):\n",
    "        if np.argmax(rs[i])==target[i]:\n",
    "            #print(np.argmax(rs[i]),target[i])\n",
    "            correct+=1\n",
    "        total+=1\n",
    "    t2=time.time()\n",
    "    print('Inference Time',t2-t1)    \n",
    "    print ('accuracy=',float(correct)/float(total))\n",
    "\n",
    "Test_number=10  # number of images  for testing procedure\n",
    "test(Test_number)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
